﻿using FlyingEye.HttpServices;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace FlyingEye.Views.DeviceOpsService
{
    public class DevicePart
    {
        public string DeviceMaintenanceId { get; set; }

        /// <summary>
        /// 部件名称
        /// </summary>
        public string DevicePartName { get; set; }

        /// <summary>
        /// 部件保养 <see cref="Consts.MaintenanceStatus"/>
        /// </summary>
        public string MaintenanceStatus { get; set; }

        // 当前寿命
        public int CurrentLifetime { get; set; }

        // 停机寿命（累计停机时长）
        public int AccumulatedDowntime { get; set; }

        // 经济寿命阈值
        public int EconomicLifetimeThreshold { get; set; }

#pragma warning disable CS8618 // 在退出构造函数时，不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
        public DevicePart()
#pragma warning restore CS8618 // 在退出构造函数时，不可为 null 的字段必须包含非 null 值。请考虑声明为可以为 null。
        {

        }
    }

    public class DevicePartManagementWindowViewModel
    {
        public ObservableCollection<string> DeviceHostIdList { get; set; } = new ObservableCollection<string>();

        public ObservableCollection<DevicePart> DeviceParts { get; set; } = new ObservableCollection<DevicePart>();
    }

    /// <summary>
    /// DevicePartManagementWindow.xaml 的交互逻辑
    /// </summary>
    public partial class DevicePartManagementWindow : Window
    {
        class IdObject
        {
            public string Id { get; set; } = string.Empty;

            public string DevicePartName { get; set; } = string.Empty;
        }

        public DevicePartManagementWindowViewModel ViewModel { get; set; } = new();

        HttpClientService _httpClientService;
        HttpUrlsOptions _url;

        public DevicePartManagementWindow(HttpClientService httpClientService,
           IOptions<HttpClientServiceOptions> options,
            IOptions<HttpUrlsOptions> httpUrlsOptions)
        {
            _url = httpUrlsOptions.Value;
            _httpClientService = httpClientService;
            options.Value.ClientId = "DeviceOpsServiceApp";
            DataContext = ViewModel;
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(DeviceHostIdTextBox.Text))
            {
                ViewModel.DeviceHostIdList.Add(DeviceHostIdTextBox.Text);
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            var newPart = new DevicePart()
            {
                DevicePartName = devicePartNameTextBox.Text,
                MaintenanceStatus = maintenanceStatusTextBox.Text,
                CurrentLifetime = int.TryParse(currentLifetimeTextBox.Text, out var currentLifetime) ? currentLifetime : 0,
                AccumulatedDowntime = int.TryParse(accumulatedDowntimeTextBox.Text, out var accumulatedDowntime) ? accumulatedDowntime : 0,
                EconomicLifetimeThreshold = int.TryParse(economicLifetimeThresholdTextBox.Text, out var economicLifetimeThreshold) ? economicLifetimeThreshold : 0
            };

            ViewModel.DeviceParts.Add(newPart);
        }

        private async void Button_Click_2(object sender, RoutedEventArgs e)
        {
            //var hosts = await _httpClientService.GetAsync<List<IdObject>>(_url.DeviceOpsServiceBaseUrl + "/api/device-ops-service/v1.0/device-hosts");
            foreach (var host in ViewModel.DeviceHostIdList)
            {
                var maintenances = await _httpClientService.GetAsync<List<IdObject>>(_url.DeviceOpsServiceBaseUrl
                    + "/api/device-ops-service/v1.0/device-hosts/" + host + "/device-maintenances");

                // https://localhost:44386/api/device-ops-service/v1.0/device-maintenances/3fa85f64-5717-4562-b3fc-2c963f66afa6/parts
                foreach (var m in maintenances)
                {
                    var parts = await _httpClientService.GetAsync<List<IdObject>>(_url.DeviceOpsServiceBaseUrl
                    + "/api/device-ops-service/v1.0/device-maintenances/" + m.Id + "/parts");

                    // https://localhost:44386/api/device-ops-service/v1.0/device-parts/3fa85f64-5717-4562-b3fc-2c963f66afa6/characteristics

                    foreach (var newPart in ViewModel.DeviceParts)
                    {
                        newPart.DeviceMaintenanceId = m.Id;

                        // https://localhost:44386/api/device-ops-service/v1.0/device-parts
                        if (!parts.Any(item => item.DevicePartName == newPart.DevicePartName))
                        {
                            await _httpClientService.PostAsync<Object, Object>(_url.DeviceOpsServiceBaseUrl
                                + "/api/device-ops-service/v1.0/device-parts", newPart);
                        }
                    }
                }
            }

            MessageBox.Show(GetWindow(this), "部件信息已提交", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
        }
    }
}
